package com.ws.webpacket.internet;

import android.util.Base64;

import com.ws.webpacket.BuildConfig;
import com.ws.webpacket.internet.been.B_Ping;
import com.ws.webpacket.internet.been.B_RootList;
import com.ws.webpacket.util.Constant;
import com.ws.webpacket.util.HLog;


import java.util.ArrayList;
import java.util.List;

public class HttpAPI {

    public static abstract class GetRootUrl extends HttpRequest<B_RootList> {

        public void startRequest() {
            Parameter parameter = new Parameter();
            parameter.setUrl(Constant.ROOT_URL);
            parameter.methodGet();
            startRequest(parameter);
        }


        @Override
        public B_RootList decodeResult(String json) throws Exception {
            HLog.I("域名列表："+json);
            B_RootList beenHost = gson.fromJson(json, B_RootList.class);

            List<String> abc = new ArrayList<>();
            List<String> hosts = new ArrayList<>();
            for (int i = 0; i < beenHost.getAbc().size(); i++) {
                String temp = new String(Base64.decode(beenHost.getAbc().get(i).getBytes(), Base64.DEFAULT));
                temp = java.net.URLDecoder.decode(temp, "utf-8");
                abc.add(temp);
                //HLog.I("initInfo  地址获取111 "+temp);
                int index = temp.indexOf("/m/#/home/?android=true");
                hosts.add(temp.substring(0, index));
            }
            beenHost.setAbc(abc);
            beenHost.setHosts(hosts);
            List<String> def = new ArrayList<>();

            //第一个地址加密
            String temp = new String(Base64.decode(beenHost.getDef().get(0).getBytes(), Base64.DEFAULT));
            temp = java.net.URLDecoder.decode(temp, "utf-8");

            if(!temp.substring(temp.length()-1).equals("/")){
                temp = temp+"/";
            }

            def.add(temp + "apps/" + BuildConfig.downloadname + beenHost.getVerson() + ".apk");
//            SYSOUtils.sOut_("+++++++++++解析到的++++++++++" + temp+ beenHost.getVerson() + ".apk");
            //HLog.I("initInfo  地址获取222 "+temp);
            try {
                // 第二个地址明码
                def.add(beenHost.getDef().get(1));
            } catch (Exception e) {

            }

            beenHost.setDef(def);
            return beenHost;

        }

    }

    /**
     * ping 主机地址寻找最快主机
     */
    public static abstract class PingRequest extends HttpRequest<B_Ping> {

        private long startTime = 0;
        private String url;
        private int index;

        //开始网络请求
        public void startRequest(String url, int index) {
            startTime = System.currentTimeMillis();
            this.url = url;
            this.index = index;

            Parameter parameter = new Parameter();
            parameter.setUrl(url);
            parameter.methodGet();
            startRequest(parameter);
        }

        @Override
        public B_Ping decodeResult(String json) throws Exception {

            B_Ping parameter = new B_Ping();
            long endTime = System.currentTimeMillis();

            parameter.setUrl(url);
            parameter.setOverTime((int) (endTime - startTime));
            parameter.setIndex(index);
            return parameter;
        }
    }


}
